var mobile="";



$(document).ready(function () {
$.jgrid.defaults.styleUI = "Bootstrap";
$("#table_list_1").jqGrid({
    data: [],
    datatype: "local",
    height: "100%",
    autowidth: true,
    shrinkToFit: true,
    rowNum: 200,
    rowList: [10, 20, 30,60,90,100,200,500],
    //colNames: ["编号", "名称", "手机号码"],
    colModel: [{
        name: 'programid', index: 'programid', align:"center",width: 20, label: "编号", align: 'center',
    },
    
    {
        name: 'group_programid', index: 'group_programid', align: "center", width: 20, label: "节目号", align: 'center',
        formatter: function (value, options, row) {
            return parseInt(value)+1;
        }
    },
    {
        name: 'festivalname', index: 'festivalname',align:"center", width: 30, label: "节目名称", align: 'center',
    },
    
    {
        name:'send_speed', index: 'send_speed',align:"center", width: 40, label: "进度(%)", align: 'center',
       formatter: function (value, options, row) {

          
               return "<div class='progress progress-mini progress-striped' style='margin:0px 10px;'><div style='width:"+value+"%;color:black;' class='progress-bar progress-bar-success'>"+value+"%</div></div>";
       }
   },
   {
    name: 'starttime', index: 'starttime',align:"center", width: 28, label: "播放开始日期", align: 'center',
    formatter: function (value, options, row) {
       
        if(typeof(value)!="undefined"){
            try{
         var date = new Date(value)
            
         return date.Format("yyyy-MM-dd");
            }catch(ex){

             console.log(ex);
            }
        }
        return "";
     }
},
{
    name: 'endtime', index: 'endtime',align:"center", width: 28, label: "播放结束日期", align: 'center',
    formatter: function (value, options, row) {
       
        if(typeof(value)!="undefined"){
            try{
         var date = new Date(value)
            
         return date.Format("yyyy-MM-dd");
            }catch(ex){

             console.log(ex);
            }
        }
        return "";
     }
},
 
],
  
pager: "#pager_list_1",
    viewrecords: true,
    multiselect:true,
    caption: "节目列表",
    hidegrid: false,
    ondblClickRow:function(row){
    $("#update").click();
    modify(row);
      // $('#myModal').modal('show')
    },
    onPaging:function(pageBtn){
        console.log(":"+pageBtn);
//	    	var re_records = $jqgrid.getGridParam('records');//获取返回的记录数
        var re_page =  $("#table_list_1").getGridParam('page');//获取返回的当前页
        var re_rowNum=  $("#table_list_1").getGridParam('rowNum');//获取每页数
        var re_total=  $("#table_list_1").getGridParam('lastpage');//获取总页数
        //alert(re_total);
        
        if(pageBtn==="next"){
           // initJqgrid(re_page+1,re_rowNum);
          // if()
           pageval=pageval+1;
           cxlist();
        }
        if(pageBtn==="prev"){
            
            pageval=pageval-1;  cxlist();
        }

        if(pageBtn=="last"){
  
            pageval=re_total;
          
            cxlist();

        }
        if(pageBtn=="first"){
  
            pageval=1;
          
            cxlist();

        }
        
        if(pageBtn=="records"){

            sizeval=$(".ui-pg-selbox").val();
            pageval=1;
            cxlist();
        }
    },

});
$("#table_list_1").setGridHeight($(window).height() * 0.73);

 $(window).resize(function(){
    $("#table_list_1").setGridWidth($(window).width());  
    $("#table_list_1").setGridHeight($(window).height() * 0.73);
});
});

var sizeval=200;
var pageval=1;
var usernameval=$("#usnameval").val();
var conameval="";
var json="";

window.cxlist=function(){
  
    console.log(sessionStorage.getItem("deviceNumber"));
    domain.ajaxUrl({
        method: 'POST',
        url: domain.testUrl+"/Threeinjection/DeviceManagement/queryDeviceInfoAndProgramInfo",
        data: {size:sizeval,page:pageval,companyId:sessionStorage.getItem("coid"),sessionid:sessionStorage.getItem("sid"),
                 "deviceNumber":sessionStorage.getItem("deviceNumber")},       
         // datatype:"JSON",
        success: function (response) {

          
             
            json=response;
            var data=JSON.parse(response);
            var reader = {
                root: function (obj) { return data.rows; },
                page: function (obj) { return pageval; },
                total: function (obj) { return data.taotal; },
                records: function (obj) {

                    return data.records;
                },
               
            };
         
    
       //$("#table_list_1").setGridParam({ data: data.rows, localReader: reader }).trigger('reloadGrid');
       $("#table_list_1").setGridParam({ data: data.rows, localReader: reader }).trigger('reloadGrid');
            var re_records = $("#table_list_1").getGridParam('records');
          
            if(re_records==0||re_records==null){
                if($(".norecords").html() == null){
                    $("#table_list_1").parent().append("<div class=\"norecords\">没有符合数据</div>");
                }
                $(".norecords").show();
            }else{

                $(".norecords").hide();
            }

         
           
            // console.log(response);
          
        }
    });
}
 





 Date.prototype.Format = function(fmt)   
{ 
//author:wangweizhen
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}


$(function(){
   // cxlist();
    $("#sxProgram").click(function(){
        // sizeval = 200;
        // pageval = 1;
        cxlist();
      

        $("#delsb").click(function(){
            window.Load();
            console.log("进来了")
        });
        
    });


  
//节目下架
$("#sbfs").click(function(){
   
    if ($("#device_id").val() == "" && $("#companyid").val() == "") {

        swal("OMG!", "请选择设备或单位", "error");
        return false;

    } else {

        var rowData = $('#table_list_1').jqGrid('getGridParam', 'selarrrow');


        if (rowData == "") {
            swal("OMG!", "请选择节目", "error");
            return false;

        }

        var programid =new Array(); var type=0;
        if (rowData.length) {
            for (var i = 0; i < rowData.length; i++) {
                programid[i]=$('#table_list_1').jqGrid('getCell', rowData[i], 'group_programid');//name是colModel中的一属性
               console.log("mnm"+parseInt(programid[i])-1)  
                //if($('#table_list_1').jqGrid('getCell',rowData[i],'type')=="""")

                var type1 = $('#table_list_1').jqGrid('getCell', rowData[i], 'devicetype');

                if (type1 == "单色") {

                    type = 0;
                }
                if (type1 == "双色") {

                    type = 1;
                }

                if (type1 == "全彩") {

                    type = 2;
                }




                if (type1 == "安卓") {

                    type = 3;
                }
            
       
        
        
        
        //接口 /DeviceManagement/deviceCommand   参数  cmd 命令码47  devicenumber设备号，多个用逗号分隔，deviceType设备类型，programGroupId节目id，companyId 公司id
        $("body").mLoading();
        var coidval = $("#companyid").val();
        if (coidval.indexOf(',') != -1) {
            coidval = coidval.split(',')[0];
        }
        $.ajax({
            type: 'POST',
            url: domain.testUrl + "/Threeinjection/DeviceManagement/deviceCommand",
            data: { cmd: 47, sessionid: sessionStorage.getItem("sid"), "programGroupId": parseInt(programid[i])-1, deviceType: type, "devicenumber": $("#device_id").val(), companyId: coidval },
            sessionid: sessionStorage.getItem("sid"),
            // timeout: 8000,//超时设置

            success: function (response) {
                response = JSON.parse(response);
                if (response.code == "200") {


                    $(".mloading-mask").hide();

                    swal("success!", "下架中，请稍等", "success");
                    $(".close").click();
                    sizeval = 200;
                    pageval = 1;
                    cxlist();
                } else {

                    $(".mloading-mask").hide();
                    swal("OMG!", "下架失败", "error");


                }
            }, error: function (response, ajaxOptions, thrownError) {

                $(".mloading-mask").hide();
                swal("OMG!", "下架失败", "error");
            },
            complete: function (XMLHttpRequest, status) {

                if (status == 'timeout') {
                    $(".mloading-mask").hide();
                    // swal("OMG!", "发送失败", "error");

                }
            }
        });
    }
}
}

    });
});
